﻿using System.Collections.Generic;

namespace InstanceController.RolesPerfMonDAL
{
	/// <summary>
	/// This class handles the communications between the View and the data store. 
	/// Generally this is the business layer logic.  The intent is for the View to issues 
	/// commands such as insert, update, or delete types. And to issue data queries to
	/// read from data storage.  
	/// It is expected that the developer will use the Class Diagram tool to create 
	/// any additional datamodels as necessary.
	/// 
	/// The code below is sample. Your code will/should describe your business intents.
	/// </summary>
	public static class CounterSettingsDataMethods
	{
		static readonly CounterSettingsDataSource CounterSettingsDataSource = new CounterSettingsDataSource(true);

		/// <summary>
		/// Saves the configuration. Fails if the record already exists.
		/// </summary>
		/// <param name="theDataModel">The data model.</param>
		/// <returns></returns>
		public static bool SaveCounterSettings(CounterSettingsDataModel theDataModel)
		{
			//Todo: Business Decision: 
			//Determine your PartitionKey plan and add the partition key now. Also you must add 
			//a unique rowKey.
			//You could simply hardcode it or use a web.config variable, you might even call out
			//to some more sophisticated partition key manager of your own design.  This is a 
			//business driven issue.
			//theDataModel.PartitionKey = theDataModel.State;			//Example: Grouping by State (a property in the datamodel)
			//theDataModel.RowKey = Guid.NewGuid().ToString("N");		//Example: Generating a Guid as the rowKey

			//Todo: Might want to do additional business logic here

			return CounterSettingsDataSource.Create(theDataModel);
		}

		/// <summary>
		/// Updates the configuration or creates a new entry if it does not exist.
		/// </summary>
		/// <param name="theDataModel">The data model.</param>
		/// <param name="overWrite">if set to <c>true</c> [over write].</param>
		/// <returns></returns>
		public static bool UpdateCounterSettings(CounterSettingsDataModel theDataModel, bool overWrite)
		{
			//Todo: Might want to do additional business logic here

			if (theDataModel == null) return false;
			if (overWrite)
			{
				if (!CounterSettingsDataSource.UpdateUnconditional(theDataModel))	//if it can't be updated create a new one
					return CounterSettingsDataSource.Create(theDataModel);

			}
			else
			{
				if (!CounterSettingsDataSource.Update(theDataModel))				//if it can't be updated create a new one
					return CounterSettingsDataSource.Create(theDataModel);
			}
			return true;
		}

		/// <summary>
		/// Deletes the configuration. Fails if the record does not exist.
		/// </summary>
		/// <param name="theDataModel">The data model.</param>
		/// <param name="unConditional">if set to <c>true</c> [unconditional].</param>
		/// <param name="asPartitionGroup">if set to <c>true</c> [aspartitiongroup].</param>
		/// <returns></returns>
		public static bool DeleteCounterSettings(CounterSettingsDataModel theDataModel, bool unConditional, bool asPartitionGroup)
		{
			//Todo: Might want to do additional business logic here

			if (true)	//add your evaluation here to determine when to delete (if ever)
			{
				if (asPartitionGroup)
				{
					if (CounterSettingsDataSource.DeletePartitionGroupUnconditional(theDataModel) > 0)
						return true;
				}
				else
					return unConditional
					       	? CounterSettingsDataSource.DeleteUnconditional(theDataModel)
					       	: CounterSettingsDataSource.Delete(theDataModel);
			}
			return true;
		}

		/// <summary>
		/// Reads the counter settings rows.
		/// </summary>
		/// <param name="partitionKey">The partition key.</param>
		/// <returns></returns>
		public static IEnumerable<CounterSettingsDataModel> ReadCounterSettingsDataModelRows(string partitionKey)
		{
			return CounterSettingsDataSource.Read(partitionKey);
		}

		/// <summary>
		/// Reads the specified row using partition key and row key.
		/// </summary>
		/// <param name="partitionKey">The partition key.</param>
		/// <param name="rowKey">The row key.</param>
		/// <returns></returns>
		public static CounterSettingsDataModel Read(string partitionKey, string rowKey)
		{
			return CounterSettingsDataSource.Read(partitionKey, rowKey);
		}

	}
}
